package com.baoxin.chenxixc4coach;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baoxin.chenxixc4coach.adapter.PotentialAdapter;
import com.baoxin.chenxixc4coach.bean.Potential;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.DensityUtil;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.MyLog;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GenjinRecordActivity extends BaseActivity implements TabLayout.OnTabSelectedListener, PotentialAdapter.EditListener {

    private TabLayout mTabLayout;
    private RecyclerView recyclerView;
    private PotentialAdapter adapter;

    private List<Potential> unList;//待跟进

    private Handler handler;
    int selected = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_genjin_record);

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        unList = (ArrayList) msg.obj;
                        adapter.setData(unList);
                        adapter.notifyDataSetChanged();
                        break;

                }
            }
        };

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//去掉默认title
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTabLayout = findViewById(R.id.tablayout);
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.tab_divider)); //设置分割线的样式
        linearLayout.setDividerPadding(DensityUtil.dp2px(this, 10));//限制分割线的高度
        mTabLayout.setOnTabSelectedListener(this);


        recyclerView = findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new PotentialAdapter(this);
        adapter.setEditListener(this);
        recyclerView.setAdapter(adapter);

        initData();
    }

    private void initData() {

        JSONObject body = new JSONObject();
        try {
            body.put("page", 1);
            body.put("limit", 20);
            body.put("ctConState", selected);
            NetUtil.post(this, Interfaces.URL_GENJIN_LIST, body.toString(), new MyCallback(Interfaces.URL_GENJIN_LIST, handler), 1);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if (unList != null) {
            unList.clear();
        }
        switch (tab.getPosition()) {
            case 0:
                selected = 0;
                initData();
                break;
            case 1:
                selected = 1;
                initData();
                break;
            case 2:
                selected = 2;
                initData();
                break;
            case 3:
                selected = 3;
                initData();
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void editRecord(Potential p) {
        Intent intent = new Intent(this, GenjinDetailActivity.class);
        intent.putExtra("potential", p);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 0) {
            initData();
        }
    }
}
